//
//  DSPSearchBar.swift
//  SWweibo
//
//  Created by 杜守鹏 on 15/12/26.
//  Copyright © 2015年 杜守鹏. All rights reserved.
//

import UIKit

class DSPSearchBar: UIView {
    
    @IBOutlet weak var textField: UITextField!
    @IBOutlet weak var cancelButton: UIButton!
    @IBOutlet weak var changeWidth: NSLayoutConstraint!
    class func searchBar()->(DSPSearchBar) {
      return   NSBundle.mainBundle().loadNibNamed("DSPSearchBar", owner: nil, options: nil).last as! DSPSearchBar
  
    }
    override func awakeFromNib() {
    textField.layer.cornerRadius = 5
        textField.layer.borderColor = UIColor.purpleColor().CGColor
        textField.layer.borderWidth = 2
        
        let imageView = UIImageView()
        imageView.frame.size = CGSize.init(width: textField.bounds.size.height, height: textField.bounds.size.height)
        let image = UIImage(named: "searchbar_textfield_search_icon")
        imageView.image = image
        imageView.contentMode = .Center
        textField.leftView = imageView
        textField.leftViewMode = .Always
        
    }

    @IBAction func didClickCancelButton(sender: UIButton) {
        // 1. 退出键盘
        self.textField.resignFirstResponder()
        // 2. 更改
        self.changeWidth.constant = 0
        UIView.animateWithDuration(0.25) { () -> Void in
            self.textField.layoutIfNeeded()
        }
        
    }
    @IBAction func didEditSearchBar(sender: AnyObject) {
       self.changeWidth.constant = self.cancelButton.frame.size.width
       UIView.animateWithDuration(0.25) { () -> Void in
       self.textField.layoutIfNeeded()
        }
    }
    
    
}
